var data = {};
$(document).ready(function () {
    //加载地址信息
    loadAddress("520600000000", function (result) {
        $.templates('#address-code-tmpl').link('#address-code', {result: result});
        //$.templates('#shipping-address-tmpl').link('#shipping-address', {result: result});
        $.templates('#delivery_area-tmpl').link('#delivery_area', {result: result});
    });
    //加载机构
    loadAffiliation();
    //取消
    $('#cancel').click(function () {
        window.location.href = 'logisticsManager.html';
    });
    var id = GetQueryString('id');
    if (id != null && id != '') {
        loadLogistics(id);//加载物流信息
    }

    $("#logistics-form").validate(
        {
            debug: true,//只验证不提交表单
            submitHandler: function (form) {
                if (id != null && id != '') {
                    updateLogistics(id);
                } else {
                    addLogistics();//表单验证通过后执行
                }
            }
        }
    );
});

//添加物流信息
function addLogistics() {
    data = {
        branchName: $('#branch-name').val().trim(),//网点名称
        branchAddressCode: $('#address-code').val().trim(),//网点地址编码
        principal: $('#principal').val().trim(),//负责人
        contact: $('#contact').val().trim(),//联系方式
        staffs: $('#staffs').val().trim(),//员工数
        vehicles: $('#vehicles').val().trim(),//车辆数
        affiliationId: $('#affiliation').val().trim(),//机构信息
       // shippingOrderId: $('#shipping-order-id').val().trim(),//运单编号
      //  shippingAddressCode: $('#shipping-address').val().trim(),//收货地址编码
        deliveryAreaCode: $('#delivery_area').val().trim(),//配送区域编码
    };
    $.ajax({
        url: '/ebb/logistics/',
        type: 'post',
        data: JSON.stringify(data),
        contentType: "application/json",
        success: function (result) {
            console.log(result);
            window.location.href = 'logisticsManager.html';
        }
    });
}

//修改物流信息
function updateLogistics(id) {
    data = {
        id: id,
        branchName: $('#branch-name').val().trim(),//网点名称
        branchAddressCode: $('#address-code').val().trim(),//网点地址编码
        principal: $('#principal').val().trim(),//负责人
        contact: $('#contact').val().trim(),//联系方式
        staffs: $('#staffs').val().trim(),//员工数
        vehicles: $('#vehicles').val().trim(),//车辆数
        affiliationId: $('#affiliation').val().trim(),//机构信息
        //shippingOrderId: $('#shipping-order-id').val().trim(),//运单编号
        //shippingAddressCode: $('#shipping-address').val().trim(),//收货地址编码
        deliveryAreaCode: $('#delivery_area').val().trim(),//配送区域编码
    };
    $.ajax({
        url: '/ebb/logistics/',
        type: 'put',
        data: JSON.stringify(data),
        contentType: "application/json",
        success: function (result) {
            console.log(result);
            window.location.href = 'logisticsManager.html';
        }
    });
}

//加载机构信息
function loadAffiliation() {
    $.ajax({
        url: '/ebb/category/range',
        type: 'get',
        data: {min: '140000000000', max: '150000000000'},
        success: function (result) {
            console.log(result);
            $.templates('#affiliation-tmpl').link('#affiliation', {result: result});
        }
    });
}

//加载物流信息
function loadLogistics(id) {
    $.ajax({
        url: '/ebb/logistics/' + id,
        type: 'get',
        success: function (result) {
            if (result != null) {
                data = result;
                $('#branch-name').val(result.branchName);//网点名称
                $('#address-code').val(result.branchAddressCode);//网点地址编码
                $('#principal').val(result.principal);//负责人
                $('#contact').val(result.contact);//联系方式
                $('#staffs').val(result.staffs);//员工数
                $('#vehicles').val(result.vehicles);//车辆数
                $('#affiliation').val(result.affiliationId);//机构信息
                $('#shipping-order-id').val(result.shippingOrderId);//运单编号
                $('#shipping-address').val(result.shippingAddressCode);//收货地址编码
                $('#delivery_area').val(result.deliveryAreaCode);//配送区域编码
            }
        }
    });
}
